package com.jetpack_sunflower


import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.*
import androidx.core.app.ShareCompat
import androidx.fragment.app.Fragment
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.google.android.material.snackbar.Snackbar
import com.jetpack_sunflower.databinding.FragmentPlantDetailBinding
import com.jetpack_sunflower.utils.InjectorUtils
import com.jetpack_sunflower.viewmodels.PlantDetailViewModel


/**
 * A simple [Fragment] subclass.
 *
 */
class PlantDetailFragment : Fragment() {
    private lateinit var shareText: String

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment

        val plantId = PlantDetailFragmentArgs.fromBundle(arguments).plantId
        val viewModelFactory = InjectorUtils.providerPlantDetailViewModelFactory(requireContext(), plantId)
        val plantDetailViewModel: PlantDetailViewModel = ViewModelProviders.of(this, viewModelFactory)
                .get(PlantDetailViewModel::class.java)

        val bi = DataBindingUtil.inflate<FragmentPlantDetailBinding>(inflater, R.layout.fragment_plant_detail, container, false)
                .apply {
                    viewModel = plantDetailViewModel
                    lifecycleOwner = this@PlantDetailFragment
                    fab.setOnClickListener {
                        plantDetailViewModel.addPlantToGarden()
                        Snackbar.make(view!!, R.string.added_plant_to_garden, Snackbar.LENGTH_LONG).show()
                    }
                    plantDetailViewModel.plant.observe(this@PlantDetailFragment, Observer { plant ->
                        shareText = if (plant == null) {
                            ""
                        } else {
                            getString(R.string.share_text_plant, plant.name)
                        }
                    })
                }

        setHasOptionsMenu(true)
        return bi.root
    }

    override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater!!.inflate(R.menu.menu_plant_detail, menu)
    }

    @SuppressLint("ObsoleteSdkInt")
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            R.id.action_share -> {
                val shareIntent = ShareCompat.IntentBuilder.from(activity)
                        .setText(shareText)
                        .setType("text/plain")
                        .createChooserIntent()
                        .apply {
                            // https://android-developers.googleblog.com/2012/02/share-with-intents.html
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                // If we're on Lollipop, we can open the intent as a document
                                addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
                            } else {
                                // Else, we will use the old CLEAR_WHEN_TASK_RESET flag
                                addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
                            }
                        }
                startActivity(shareIntent)
                return true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

}
